package com.huawei.hianalytics.f.f;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Pair;
import com.huawei.hianalytics.f.b.b;
import com.huawei.hianalytics.f.b.c;
import com.huawei.hianalytics.f.b.d;
import com.huawei.hianalytics.f.b.g;
import com.huawei.hianalytics.f.b.h;
import com.huawei.hianalytics.f.b.i;
import com.huawei.hianalytics.f.g.f;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;

public class o {

    static class a implements com.huawei.hianalytics.f.d.a.a {
        private h a;

        public a(h hVar) {
            this.a = hVar;
        }

        public void a(long j, byte[] bArr) {
            this.a.g(String.valueOf(j));
            this.a.f(f.a(bArr));
        }
    }

    private static b a(String str, String str2) {
        b bVar = new b();
        a(bVar, str, str2);
        return bVar;
    }

    static g a(List<com.huawei.hianalytics.f.b.a> list, String str, String str2, String str3, String str4, Context context) {
        JSONObject jSONObject;
        g gVar;
        b a;
        JSONObject jSONObject2 = null;
        com.huawei.hianalytics.f.c.a a2 = a(context);
        if (a2 != null) {
            g gVar2 = new g(a2.a(), str2, str);
            SharedPreferences d = com.huawei.hianalytics.f.g.h.d(context, "common_nc");
            String k = ((com.huawei.hianalytics.f.b.a) list.get(0)).k();
            if (TextUtils.isEmpty(k)) {
                com.huawei.hianalytics.g.b.b("ReportDataHandle", "evtExHashCode is empty.this is old data");
                jSONObject = null;
            } else {
                String str5 = (String) com.huawei.hianalytics.f.g.h.b(d, k, "");
                if (TextUtils.isEmpty(str5)) {
                    com.huawei.hianalytics.g.b.b("ReportDataHandle", "nc_common SP : evtExConfig is empty,hashCode: " + k);
                    jSONObject = null;
                } else {
                    try {
                        JSONObject jSONObject3 = new JSONObject(str5);
                        try {
                            jSONObject = jSONObject3.getJSONObject("headerEx");
                            jSONObject2 = jSONObject3;
                        } catch (JSONException e) {
                            jSONObject = jSONObject3;
                            com.huawei.hianalytics.g.b.c("ReportDataHandle", "evtExConfig is error : nc_common sp ");
                            jSONObject2 = jSONObject;
                            jSONObject = null;
                            gVar2.a(a(a2.b(), str2, str, str4, jSONObject));
                            gVar = gVar2;
                            a = a(str, str2);
                            if (jSONObject2 != null) {
                                try {
                                    a.a(jSONObject2.getJSONObject("commonEx"));
                                } catch (JSONException e2) {
                                    com.huawei.hianalytics.g.b.c("ReportDataHandle", "setEvtExCommon：evtExConfig is error : nc_common sp ");
                                }
                            }
                            gVar.a(a);
                            gVar.a(a(str, str2, str3));
                            gVar.a((List) list);
                            return gVar;
                        }
                    } catch (JSONException e3) {
                        jSONObject = null;
                        com.huawei.hianalytics.g.b.c("ReportDataHandle", "evtExConfig is error : nc_common sp ");
                        jSONObject2 = jSONObject;
                        jSONObject = null;
                        gVar2.a(a(a2.b(), str2, str, str4, jSONObject));
                        gVar = gVar2;
                        a = a(str, str2);
                        if (jSONObject2 != null) {
                            a.a(jSONObject2.getJSONObject("commonEx"));
                        }
                        gVar.a(a);
                        gVar.a(a(str, str2, str3));
                        gVar.a((List) list);
                        return gVar;
                    }
                }
            }
            gVar2.a(a(a2.b(), str2, str, str4, jSONObject));
            gVar = gVar2;
        } else {
            gVar = new g();
        }
        a = a(str, str2);
        if (jSONObject2 != null) {
            a.a(jSONObject2.getJSONObject("commonEx"));
        }
        gVar.a(a);
        gVar.a(a(str, str2, str3));
        gVar.a((List) list);
        return gVar;
    }

    private static h a(String str, String str2, String str3, String str4, JSONObject jSONObject) {
        h hVar = new h();
        hVar.c(str);
        hVar.e(com.huawei.hianalytics.a.b.f());
        hVar.b(str2);
        hVar.a(str4);
        hVar.a(jSONObject);
        StringBuffer stringBuffer = new StringBuffer("hmshi");
        stringBuffer.append(str3).append("qrt");
        hVar.d(stringBuffer.toString());
        try {
            com.huawei.hianalytics.f.d.a.a(hVar.a(), com.huawei.hianalytics.a.b.b(com.huawei.hianalytics.d.a.a().g(str2, str3)), new a(hVar));
        } catch (NoSuchAlgorithmException e) {
            com.huawei.hianalytics.g.b.c("HiAnalytics/event", "generateHeadData(): NoSuchAlgorithmException");
        } catch (UnsatisfiedLinkError e2) {
            com.huawei.hianalytics.g.b.c("HiAnalytics/event", "generateHeadData(): UnsatisfiedLinkError");
        }
        return hVar;
    }

    private static i a(String str, String str2, String str3) {
        i iVar = new i();
        a(iVar, str, str2, str3);
        return iVar;
    }

    private static com.huawei.hianalytics.f.c.a a(Context context) {
        Closeable open;
        Closeable closeable = null;
        com.huawei.hianalytics.f.c.a b = i.a().b();
        long c = b.c();
        String a;
        if (c == 0) {
            try {
                com.huawei.hianalytics.g.b.b("HiAnalytics/event", "first read pubKey from config");
                open = context.getResources().getAssets().open("hianalytics_njjn");
                try {
                    a = com.huawei.hianalytics.util.f.a((InputStream) open, 2048);
                    if (TextUtils.isEmpty(a)) {
                        com.huawei.hianalytics.g.b.c("HiAnalytics/event", "rsa publicKey is empty");
                        com.huawei.hianalytics.util.f.a(7, open);
                        return null;
                    }
                    c = System.currentTimeMillis();
                    String a2 = f.a();
                    String a3 = com.huawei.hianalytics.f.g.g.a(a, a2);
                    b.a(c);
                    b.a(a2);
                    b.c(a);
                    b.b(a3);
                } catch (IOException e) {
                    try {
                        com.huawei.hianalytics.g.b.c("HiAnalytics/event", "read pubKey error,the file is corrupted");
                        com.huawei.hianalytics.util.f.a(7, open);
                        return null;
                    } catch (Throwable th) {
                        Throwable th2 = th;
                        closeable = open;
                        Throwable th3 = th2;
                        com.huawei.hianalytics.util.f.a(7, closeable);
                        throw th3;
                    }
                }
            } catch (IOException e2) {
                open = null;
                com.huawei.hianalytics.g.b.c("HiAnalytics/event", "read pubKey error,the file is corrupted");
                com.huawei.hianalytics.util.f.a(7, open);
                return null;
            } catch (Throwable th4) {
                th3 = th4;
                com.huawei.hianalytics.util.f.a(7, closeable);
                throw th3;
            }
        }
        long currentTimeMillis = System.currentTimeMillis();
        if (currentTimeMillis - c > 43200000) {
            com.huawei.hianalytics.g.b.b("HiAnalytics/event", "work key refresh");
            String a4 = f.a();
            a = com.huawei.hianalytics.f.g.g.a(b.d(), a4);
            b.a(currentTimeMillis);
            b.a(a4);
            b.b(a);
        }
        open = null;
        com.huawei.hianalytics.util.f.a(7, open);
        return b;
    }

    static List<d> a(c[] cVarArr) {
        List<d> arrayList = new ArrayList(cVarArr.length);
        for (c fVar : cVarArr) {
            arrayList.add(new com.huawei.hianalytics.f.b.f(fVar));
        }
        return arrayList;
    }

    private static void a(b bVar, String str, String str2) {
        if (bVar != null) {
            String str3;
            String str4 = "";
            String str5 = "";
            String str6 = "";
            String d = com.huawei.hianalytics.d.a.a().d(str2, str);
            com.huawei.hianalytics.c.a a = com.huawei.hianalytics.d.a.a().a(str2, str);
            switch (a.a()) {
                case SN:
                    str6 = a.b();
                    str3 = str4;
                    str4 = str5;
                    str5 = str6;
                    break;
                case IMEI:
                    str3 = a.b();
                    str4 = str5;
                    str5 = str6;
                    break;
                case UDID:
                    str5 = a.b();
                    str3 = str4;
                    str4 = str5;
                    str5 = str6;
                    break;
                default:
                    str3 = str4;
                    str4 = str5;
                    str5 = str6;
                    break;
            }
            bVar.a(d);
            boolean j = com.huawei.hianalytics.a.c.j(str2, str);
            str6 = "";
            if (j) {
                str6 = com.huawei.hianalytics.a.c.k(str2, str);
                if (TextUtils.isEmpty(str6)) {
                    str6 = (String) com.huawei.hianalytics.f.g.h.b(com.huawei.hianalytics.f.g.h.d(com.huawei.hianalytics.a.b.n(), "global_v2"), str2, "");
                    com.huawei.hianalytics.a.c.a(str6, str2, str);
                }
                if (TextUtils.isEmpty(str6)) {
                    str6 = com.huawei.hianalytics.a.b.b(j);
                }
            }
            bVar.c(str6);
            bVar.b(str3);
            bVar.d(com.huawei.hianalytics.d.a.a().c(str2, str));
            str6 = com.huawei.hianalytics.a.c.a(str2, str);
            bVar.e(str4);
            bVar.g(str5);
            bVar.f(str6);
        }
    }

    private static void a(i iVar, String str, String str2, String str3) {
        iVar.c(com.huawei.hianalytics.a.b.c());
        Pair b = com.huawei.hianalytics.d.a.a().b(str2, str);
        iVar.h(com.huawei.hianalytics.util.f.a());
        iVar.f((String) b.first);
        iVar.g((String) b.second);
        iVar.d(com.huawei.hianalytics.a.b.e());
        iVar.e(str3);
        iVar.b(com.huawei.hianalytics.d.a.a().e(str2, str));
        iVar.a(com.huawei.hianalytics.d.a.a().f(str2, str));
    }
}
